package com.jd.org.controller;

import com.jd.basic.anno.LogAnnotation;
import com.jd.basic.anno.PreAuthorize;
import com.jd.basic.utils.AjaxResult;
import com.jd.basic.utils.PageList;
import com.jd.org.domain.Department;
import com.jd.org.query.DepartmentQuery;
import com.jd.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 小林同学
 * @version 1.0
 * @description: TODO
 * @date 2023/2/18 21:01
 */
@RestController
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @LogAnnotation("查询部门列表")
    @GetMapping
    public List<Department> findAll(){

        return departmentService.findAll();
    }
    /**
     * 根据id查询部门
     * @author kabuda
     * @date 2023/2/21 12:50
     * @return Department
    */
    @LogAnnotation("根据id查询部门")
    @GetMapping("/{id}")
    @PreAuthorize(sn = "dept:list",name = "根据id查询")
    public Department findById(@PathVariable("id") Long id) {
        return  departmentService.findById(id);
    }
    /**
     * 增加和修改
     * @author kabuda
     * @date 2023/2/21 17:53
     * @return AjaxResult
    */
    @LogAnnotation("部门的增加或者修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (null == department.getId()) {
                departmentService.add(department);
            } else {
                departmentService.update(department);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
        return AjaxResult.me().success();
    }
    /**
     * 删除
     * @author kabuda
     * @date 2023/2/21 17:58
     * @return AjaxResult
    */

    @LogAnnotation("批量删除部门")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            departmentService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
        return AjaxResult.me().success();
    }
    @LogAnnotation("删除部门")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id ){
        departmentService.delete(id);
        return AjaxResult.me().success();
    }
    @LogAnnotation("分页查询部门列表")
    @PostMapping
    public PageList queryByPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryByPage(departmentQuery);
    }
    /**
     * 获取上级部门
     * @author kabuda
     * @date 2023/2/23 10:50
     * @return List<Department>
    */
    @LogAnnotation("获取上级部门")
    @GetMapping("/tree")
    public List<Department> getParent(){
        return departmentService.getParent();
    }
}
